﻿package gb {
	import flash.display.MovieClip;
	public class PhysicalCore {
		
		private var subject:MovieClip;
		public var x:Number;
		public var y:Number;
		
		public var pitch:Number = 0;
		
		
		public var velocityX:Number;
		//public var velocityY:Number;
		public var velocityUp:Number;
		
		public var gravity = 3;
		public var friction = 0;
		
		public function PhysicalCore(sbj:MovieClip) {
			// constructor code
			this.subject = sbj;
			velocityUp = 0;
			velocityX = 0;
		}
		protected function PhysicalEffect():Boolean
		{
			if(velocityX!=0)
			{
				x += velocityX;
				velocityX = Forward(velocityX,0,friction);
			}
			if(velocityUp != 0 || pitch > 0)
			{
				pitch+= velocityUp;
				velocityUp-=gravity;
				if(velocityUp <= 0 && pitch <= 0)
				{
					pitch = 0;
					velocityUp = 0;
					return true;
				}
			}
			return false;
		}
		
		public final function Update():Boolean
		{
			var isContacted = PhysicalEffect();
			
			if(!Constant.IS_SCROLLING_MODE)
				subject.x = x;
			subject.y = y - pitch;
			return isContacted;
		}		
		
		public function IsOverground():Boolean
		{
			return (pitch == 0 && velocityUp == 0);
		}
		
		public static function Forward(s:Number,t:Number,d:Number):Number
		{
			var r;
			if(t>s)
			{
				r = s + d;
				if(r>t)
				{
					return t;
				}
				else
				{
					return r;
				}
			}
			else
			{
				r = s - d;
				if(r<t)
				{
					return t;
				}
				else
				{
					return r;
				}
			}
		}
	}
	
}
